﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Xml.Serialization;
using WebChatApi.Common.Interface;

namespace WebChatApi.Common
{
    /// <summary>
    /// 
    /// </summary>
    [DataContract]
    public sealed class DynamicPropertyMappingCollection : IEnumerable<DynamicPropertyMapping>
    {
        /// <summary>
        /// The mappings
        /// </summary>
        private List<DynamicPropertyMapping> mappings = new List<DynamicPropertyMapping>();

        public int MappingCount
        {
            get
            {
                return mappings.Count;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DynamicPropertyMappingCollection"/> class.
        /// </summary>
        public DynamicPropertyMappingCollection()
        {
        }

        #region Methods

        /// <summary>
        /// Adds the property mapping.
        /// </summary>
        /// <param name="mapping">The mapping.</param>
        /// <param name="checkDuplicate">if set to <c>true</c> [check duplicate].</param>
        public void AddPropertyMapping(DynamicPropertyMapping mapping, bool checkDuplicate = false)
        {
            if (mapping != null)
            {
                if (checkDuplicate)
                {
                    var result = from item in mappings where item.SourceName == mapping.SourceName && item.PropertyName == mapping.PropertyName select item;
                    if (result.Count() == 0)
                    {
                        mappings.Add(mapping);
                    }
                }
                else
                {
                    mappings.Add(mapping);
                }
            }
        }

        /// <summary>
        /// Gets the name of the mapping by source.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public DynamicPropertyMapping GetMappingBySourceName(string name)
        {
            DynamicPropertyMapping result = null;
            if (!string.IsNullOrWhiteSpace(name))
            {
                result = (from item in mappings where item.SourceName == name select item).FirstOrDefault();
            }
            return result;
        }

        /// <summary>
        /// Gets the name of the mapping by property.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public DynamicPropertyMapping GetMappingByPropertyName(string name)
        {
            DynamicPropertyMapping result = null;
            if (!string.IsNullOrWhiteSpace(name))
            {
                result = (from item in mappings where item.PropertyName == name select item).FirstOrDefault();
            }
            return result;
        }

        #endregion

        #region IXmlSerializable


        #endregion

        #region IEnumerable

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1" /> that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator<DynamicPropertyMapping> GetEnumerator()
        {
            return mappings.GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.
        /// </returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return mappings.GetEnumerator();
        }

        #endregion
    }
}
